package com.wtgroup.demo.leetcode.q019;

import com.wtgroup.demo.common.bean.LinkNode;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/4/2 10:42
 */
public class S_LC_QuickSlowPointer {
    /**
     * 快慢指针
     */

    public LinkNode removeNthFromEnd(LinkNode head, int n) {
        if (head==null) {
            return head;
        }

        LinkNode quick=head, slow=head;
        // 初始化快指针, 下标 n
        while (n > 0) {
            quick = quick.next;
            n--;
        }

        // head 即删除目标
        if (quick==null) {
            return head.next;
        }

        for (; quick.next != null; quick = quick.next, slow = slow.next) {}
        slow.next = slow.next.next;

        return head;
    }





}
